package cn.jobstart.data.liePin;

import cn.jobstart.common.FileUtil;
import cn.jobstart.common.SkList;
import cn.jobstart.common.StringUtil;
import cn.jobstart.props.FileConfig;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * @author sunke
 * @version 1.0
 * @Description
 * @date 2023-12-07 10:39
 */

@Component
public class CityUtil {
    @Resource
    private FileConfig fileConfig;


    private List<City> cityList;


    private void init() {
        try {
            String data = FileUtil.readInFile(fileConfig.getRoot() + File.separator + "lie_pin" + File.separator + "city.js", "utf-8");

            JSONObject jsonObject = JSONObject.parseObject(data);

            JSONObject cityJson = jsonObject.getJSONObject("city").getJSONObject("list");
            JSONObject provinceJson = jsonObject.getJSONObject("province").getJSONObject("list");
            cityList = SkList.getInstance();
            for (String province : provinceJson.keySet()) {
                try {
                    City city = new City();
                    JSONObject item = provinceJson.getJSONObject(province);
                    city.setName(item.getString("name"));
                    city.setCode(item.getString("code"));
                    city.setParentCode(item.getString("parentCode"));
                    city.setAreaNumber(item.getString("areaNumber"));

                    cityList.add(city);
                } catch (Exception e) {

                }

            }
            for (String cityItem : cityJson.keySet()) {
                try {
                    City city = new City();
                    JSONObject item = cityJson.getJSONObject(cityItem);
                    city.setName(item.getString("name"));
                    city.setCode(item.getString("code"));
                    city.setParentCode(item.getString("parentCode"));
                    city.setAreaNumber(item.getString("areaNumber"));


                    cityList.add(city);
                } catch (Exception e) {

                }

            }


        } catch (Exception e) {

        }

    }

    public String getCodeByName(String name) {

        if (StringUtil.isNullOrEmpty(name)) {
            return null;
        }

        if (StringUtil.isNullOrEmpty(cityList)) {
            init();
        }
        if (StringUtil.isNullOrEmpty(cityList)) {
            return null;
        }
        String nameItem = name.replace("市", "");
        nameItem = nameItem.replace("省", "");
        for (City city : cityList) {
            String cityName = city.getName();
            if (StringUtil.isNullOrEmpty(cityName)) {
                continue;
            }
            cityName = cityName.replace("市", "");
            cityName = cityName.replace("省", "");
            if (nameItem.equalsIgnoreCase(cityName)) {
                return city.getCode();
            }

        }


        return "";
    }


}
